package com.lcp.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lcp.common.Result;
import com.lcp.entity.TcmKnowledgeComment;
import com.lcp.service.TcmKnowledgeCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/tcm-knowledge-comments")
public class TcmKnowledgeCommentController {

    @Autowired
    private TcmKnowledgeCommentService tcmKnowledgeCommentService;

    @GetMapping("/{id}")
    public Result<TcmKnowledgeComment> getById(@PathVariable Long id) {
        return Result.success(tcmKnowledgeCommentService.getById(id));
    }

    @GetMapping("/page")
    public Result<IPage<TcmKnowledgeComment>> pageByKnowledgeId(
            @RequestParam Long knowledgeId,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        return Result.success(tcmKnowledgeCommentService.pageByKnowledgeId(knowledgeId, pageNum, pageSize));
    }

    @GetMapping("/children")
    public Result<List<TcmKnowledgeComment>> listByParentId(@RequestParam Long parentId) {
        return Result.success(tcmKnowledgeCommentService.listByParentId(parentId));
    }

    @PostMapping
    public Result<TcmKnowledgeComment> create(@RequestBody TcmKnowledgeComment comment) {
        return Result.success(tcmKnowledgeCommentService.create(comment));
    }

    @PutMapping("/{id}")
    public Result<TcmKnowledgeComment> update(@PathVariable Long id, @RequestBody TcmKnowledgeComment comment) {
        comment.setId(id);
        return Result.success(tcmKnowledgeCommentService.update(comment));
    }

    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Long id) {
        tcmKnowledgeCommentService.delete(id);
        return Result.success();
    }

    @PostMapping("/{id}/like")
    public Result<Void> like(@PathVariable Long id) {
        tcmKnowledgeCommentService.incrementLikeCount(id);
        return Result.success();
    }
} 